package com.aiyige.base.db.dao;

import android.util.Log;

import com.aiyige.base.db.DBHelper;
import com.aiyige.base.db.table.UploadFile;
import com.aiyige.page.publish.util.PublishUtil;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.sql.SQLException;

/**
 * Created by zhuyakun on 2017/11/15.
 */

public class UploadFileDao {
    public static final String TAG = UploadFileDao.class.getSimpleName();
    public static Dao<UploadFile, Integer> dao = null;

    public static Dao<UploadFile, Integer> getDao() {
        if (dao == null) {
            try {
                dao = DBHelper.getInstance().getDao(UploadFile.class);
            } catch (SQLException e) {
                Log.e(TAG, "getDao:" + Log.getStackTraceString(e));
            }
        }
        return dao;
    }

    public static void resumeById(Integer id) {
        try {
            UpdateBuilder<UploadFile, Integer> updateBuilder = UploadFileDao.getDao().updateBuilder();
            updateBuilder.updateColumnValue(UploadFile.UPLOAD_TIMESTAMP_FIELD, 0);
            updateBuilder.updateColumnValue(UploadFile.RETRY_COUNT_FIELD, 0);
            updateBuilder.updateColumnValue(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_UPLOAD);
            updateBuilder.where()
                    .eq(UploadFile.ID_FIELD, id)
                    .and()
                    .ne(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_DELETE);
            updateBuilder.update();
            PublishUtil.startPublish();
        } catch (Exception e) {

        }
    }

    public static void resumeByParentId(String parentId) {
        try {
            UpdateBuilder<UploadFile, Integer> updateBuilder = UploadFileDao.getDao().updateBuilder();
            updateBuilder.updateColumnValue(UploadFile.UPLOAD_TIMESTAMP_FIELD, 0);
            updateBuilder.updateColumnValue(UploadFile.RETRY_COUNT_FIELD, 0);
            updateBuilder.updateColumnValue(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_UPLOAD);
            updateBuilder.where()
                    .eq(UploadFile.PARENT_ID_FIELD, parentId)
                    .and()
                    .ne(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_DELETE);
            updateBuilder.update();
            PublishUtil.startPublish();
        } catch (Exception e) {

        }
    }


    public static void pauseById(Integer id) {
        try {
            UpdateBuilder<UploadFile, Integer> updateBuilder = UploadFileDao.getDao().updateBuilder();
            updateBuilder.updateColumnValue(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_PAUSE);
            updateBuilder.where()
                    .eq(UploadFile.ID_FIELD, id)
                    .and()
                    .ne(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_DELETE);
            updateBuilder.update();
        } catch (Exception e) {

        }
    }

    public static void pauseByParentId(String parentId) {
        try {
            UpdateBuilder<UploadFile, Integer> updateBuilder = UploadFileDao.getDao().updateBuilder();
            updateBuilder.updateColumnValue(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_PAUSE);
            updateBuilder.where()
                    .eq(UploadFile.PARENT_ID_FIELD, parentId)
                    .and()
                    .ne(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_DELETE);
            updateBuilder.update();
        } catch (Exception e) {

        }
    }

    public static void deleteById(Integer id) {
        try {
            UpdateBuilder<UploadFile, Integer> updateBuilder = UploadFileDao.getDao().updateBuilder();
            updateBuilder.updateColumnValue(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_DELETE);
            updateBuilder.where().eq(UploadFile.ID_FIELD, id);
            updateBuilder.update();
        } catch (Exception e) {

        }
    }

    public static void deleteByParentId(String parentId) {
        try {
            UpdateBuilder<UploadFile, Integer> updateBuilder = UploadFileDao.getDao().updateBuilder();
            updateBuilder.updateColumnValue(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_DELETE);
            updateBuilder.where().eq(UploadFile.PARENT_ID_FIELD, parentId);
            updateBuilder.update();
        } catch (Exception e) {

        }
    }

    public static void pauseAll() {
        try {
            UpdateBuilder<UploadFile, Integer> updateBuilder = UploadFileDao.getDao().updateBuilder();
            updateBuilder.updateColumnValue(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_PAUSE);
            updateBuilder.where().ne(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_DELETE);
            updateBuilder.update();
        } catch (Exception e) {

        }
    }

}
